<!--
* description: 客户档案-客户车辆关系
* author:cjw
* createdDate: 2021.1.21
-->
<template>
  <div class="app-container app-container-table">
    <one-table-template
      ref="multipleTable"
      :dynamic-buttons="tableButtons"
      :dynamic-components="tableComponents"
      :dynamic-api-config="apiConfig"
      :dynamic-table-cols="tableCols"
      :dynamic-form-fields="formField"
      :dynamic-is-show-select="false"
      :dynamic-is-show-more-btn="true"
      :callback-obj="callbackObj"
    />

    <edit
      :key="editPopupsKey"
      :dynamic-edit-row-data="editRowData"
      :popups-visible.sync="editPopupsVisible"
      :popups-state="editPopupsState"
    />
    <carSync
      :key="carSyncPopupsKey"
      :data-for-form="carSyncRowData"
      :popups-visible.sync="carSyncPopupsVisible"
      :popups-state="carSyncPopupsState"
    />
  </div>
</template>
<script>
import { oneTableWithViewTemplateMixins } from '@/components/mixins/oneTableWithViewTemplateMixins'
import OneTableTemplate from '@/components/templates/oneTable'
import { managements } from '@/api/se/bookingManagement'
import { exportExcel } from '@/utils/getTimeString'
import Vue from 'vue'

export default {
  name: 'Customerelationshipquery',
  components: {
    OneTableTemplate,
    Edit: () => import('./edit'),
    carSync: () => import('./carSync')
  },
  mixins: [oneTableWithViewTemplateMixins],
  // 阻塞路由预加载网格中组件的数据
  beforeRouteEnter(to, from, next) {
    Vue.prototype.$ConfigCache.CacheConfig.initData(
      [to.path, 'supplierMessage-edit'],
      () => {
        next()
      }
    )
  },

  data() {
    // 页面标识（与上面beforeRouteEnter中的对应）
    const viewPageCode = this.$route.path
    // 绑定事件配置化事件
    this.$ConfigCache.CacheConfig.bindEvent(this, [viewPageCode])
    return {
      editRowData: {},
      editPopupsVisible: false,

      carSyncPopupsKey: 'carSyncPopupsKey',
      carSyncRowData: {},
      carSyncPopupsState: '',
      carSyncPopupsVisible: false,

      // 网格查询API配置对象
      apiConfig: managements.queryCustAndCar,
      // 动态组件-按钮
      tableButtons: [
        {
          compKey: 'btnKey1',
          // type: 'primary',
          size: 'small',
          clickEvent: () => this.add(),
          text: '他牌车维护', // 查询
          // name: 'search', // 按钮图标样式
          position: 'right',
          fuzzySearch: true,
        },
        {
          compKey: 'btnKey2',
          // type: 'primary',
          size: 'small',
          clickEvent: () => this.carSync(),
          text: '车辆信息同步', // 查询
          // name: 'search', // 按钮图标样式
          position: 'right',
          fuzzySearch: true,
        },
        {
          compKey: 'btnKey3',
          type: 'primary',
          size: 'small',
          clickEvent: () => this.queryTable(1),
          text: this.$t('sys.button.query'), // 查询
          name: 'search', // 按钮图标样式
          position: 'right',
          fuzzySearch: true
        },
        {
          compKey: 'btnKey4',
          size: 'small',
          clickEvent: () => this.reset(),
          text: this.$t('sys.button.reset'),
          name: 'reset',
          position: 'right' // 重置
        },
        {
          compKey: 'btnKey5',
          size: 'small',
          clickEvent: () => this.exportExcelAll(),
          text: '导出', // err
          name: 'export',
          position: 'left'
        }
      ],
      // 动态组件-查询条件
      tableComponents:
        this.$ConfigCache.CacheConfig.cacheData[this.$route.path] &&
        this.$ConfigCache.CacheConfig.cacheData[this.$route.path]
          .tableComponents &&
        this.$ConfigCache.CacheConfig.cacheData[this.$route.path]
          .tableComponents.length > 0
          ? this.$ConfigCache.CacheConfig.cacheData[this.$route.path]
            .tableComponents
          : [
            // 显示组件
            {
              compKey: 'compKey1',
              labelName: 'VIN码：',
              codeField: 'vin',
              isMul: false,
              component: () => import('@/components/org/commonInput'),
              type: 'inputText',
              isMust: true
            },
            {
              compKey: 'compKey2',
              labelName: '客户姓名：',
              codeField: 'custName',
              component: () => import('@/components/org/commonInput'),
              type: 'inputText',
              isMul: false,
              isMust: true
            },
            {
              compKey: 'compKey3',
              labelName: '车牌号：',
              codeField: 'carLicense',
              component: () => import('@/components/org/commonInput'),
              type: 'inputText',
              isMul: false,
              isMust: true
            },
            {
              compKey: 'compKey4',
              labelName: '联系电话：',
              codeField: 'phone',
              component: () => import('@/components/org/commonInput'),
              type: 'inputText',
              isMust: true
            }
          ],
      // 动态生成网格列
      tableCols:
        this.$ConfigCache.CacheConfig.cacheData[this.$route.path] &&
        this.$ConfigCache.CacheConfig.cacheData[this.$route.path].tableCols &&
        this.$ConfigCache.CacheConfig.cacheData[this.$route.path].tableCols
          .length > 0
          ? this.$ConfigCache.CacheConfig.cacheData[this.$route.path].tableCols
          : [
            {
              prop: 'controlBtn',
              label: '操作', // 操作
              codeField: 'controlBtn',
              align: 'center',
              isComponent: true,
              comps: [
                {
                  compKey: 'propKey0',
                  align: 'center',
                  labelName: '详情',
                  clickEvent: (index) => {
                    this.edit(index)
                  },
                  component: () => import('@/components/org/linkButton')
                }
              ]
            },
            {
              prop: 'vin',
              label: 'VIN码'
            },
            {
              prop: 'carLicense',
              label: '车牌号'
            },
            {
              prop: 'custNo',
              label: '用户ID'
            },
            {
              prop: 'custName',
              label: '客户姓名'
            },
            {
              prop: 'phone',
              label: '联系电话'
            },
            {
              prop: 'carSeriesCode',
              label: '车系'
            },
            {
              prop: 'carConfigCode',
              label: '车型'
            },
            {
              prop: 'carConfigCn',
              label: '车型名称'
            },
          ],

      // 表单查询数据
      formField: this.$utils.getFormField(
        this,
        {
          vin: '',
          custName: '',
          carLicense: '',
          phone: ''
        },
        this.$ConfigCache.CacheConfig.cacheData[viewPageCode]
      ),
      // 单表格模版页面组件回调事件
      callbackObj: {
      }
    }
  },
  mounted() {
    this.queryTable(1)
  },
  created(){
    this.dlrTypeQuery();
  },
  methods: {
    // 查询dlrType
    dlrTypeQuery(){
      const that = this
      const queryObj = {
        // 保存mutation
        type: 'mutation',
        // api配置
        apiConfig: managements.queryCustAndCar,
        // 条件/实体参数（变量），根据typeParam中的定义配置
        variables: {}
      }
      // 转换了中台请求格式数据
      var paramD = this.$getParams(queryObj)
      // 调用中台API方法（可复用）
      this.$requestAPI(paramD).then(response => {
        if (response.result === '1') {
          if(response.rows[0].dlrType==="1"){
            this.tableButtons[0].isShow=true;
            this.tableButtons[1].isShow=false;

            this.tableButtons[0].compKey=this.tableButtons[0].compKey+"1";
            this.tableButtons[1].compKey=this.tableButtons[1].compKey+"1";
          }else {
            this.tableButtons[0].isShow=false;
            this.tableButtons[1].isShow=true;

            this.tableButtons[0].compKey=this.tableButtons[0].compKey+"1";
            this.tableButtons[1].compKey=this.tableButtons[1].compKey+"1";
          }
        }
      })
    },
    edit(index) {
      const that = this
      const queryObj = {
        // 保存mutation
        type: 'mutation',
        // api配置
        apiConfig: managements.queryCustAndCars,
        // 条件/实体参数（变量），根据typeParam中的定义配置
        variables: {
          custNo: this.$refs['multipleTable'].list[index].custNo,
          flag: this.$refs['multipleTable'].list[index].flag,
          custCarId: this.$refs['multipleTable'].list[index].custCarId,
        }
      }
      // 转换了中台请求格式数据
      var paramD = this.$getParams(queryObj)
      // 调用中台API方法（可复用）
      this.$requestAPI(paramD).then(response => {
        if (response.result === '1') {
          this.editRowData = Object.assign({}, response.rows[0], this.$refs['multipleTable'].list[index])
          this.editPopupsState = 'edit'
          this.editPopupsVisible = true
          this.editPopupsKey = this.$utils.generateId()
        }
      })
    },

    add() {
      this.editPopupsState = 'add'
      this.editPopupsVisible = true
      this.editPopupsKey = this.$utils.generateId()
    },
    carSync() {
      this.carSyncPopupsVisible = true
    },
    
    exportExcelAll() {
      exportExcel(this.formField, managements.exporte.APIUrl, managements.exporte.ServiceCode, '客户车辆关系')
    }
  }
}
</script>
<style lang='scss' scoped>


</style>
